package pl.edu.agh.logview.database;


import java.sql.SQLException;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;

import pl.edu.agh.logview.model.Agent;
import pl.edu.agh.logview.model.Message;
import pl.edu.agh.logview.model.State;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.jdbc.JdbcConnectionSource;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

public class DbHelper {
	
	private static ConnectionSource connectionSource;
	
	public DbHelper() {
		try {
			connectionSource = new JdbcConnectionSource(new XMLConfiguration("conf/configuration.xml").getString("DB"));
			TableUtils.createTableIfNotExists(connectionSource, Agent.class);
			TableUtils.createTableIfNotExists(connectionSource, Message.class);
			TableUtils.createTableIfNotExists(connectionSource, State.class);
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ConfigurationException e) {
			e.printStackTrace();
		}
	}
	
	public Dao<Agent,String> getAgentDao() {
		try {
			return DaoManager.createDao(connectionSource, Agent.class);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public Dao<Message, Long> getMessageDao() {
		try {
			return DaoManager.createDao(connectionSource, Message.class);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public Dao<State, Long> getStateDao() {
		try {
			return DaoManager.createDao(connectionSource, State.class);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
}
